package org.example;

import org.example.excel.ExcelWriter;
import org.example.pojo.BOM;

import org.example.pojo.Member;
import org.example.read.ReadDomains;
import org.example.xml.Dom4jExample;

import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * 本文件可实现将规则引擎的动态域配置转为动态域文件
 */
public class BomToExcel {
    private static final String SHARP = "#";

    public static void main(String[] args) {
        Map<String, BOM> bomMap = null;
        //1.解析动态域.b2x
        String xmlPath = "D:\\01MyFile\\赵政\\NBBOM\\bom\\动态域.b2x";
        System.out.println("=================开始解析：动态域.b2x=======================================");
        bomMap = Dom4jExample.xmlToPojo(xmlPath);
       /* for(Map.Entry<String,BOM> entry:bomMap.entrySet()){
            System.out.println(entry.getKey());
            System.out.println(entry.getValue());
        }*/

        //2.解析动态域_zh_CN.voc
        String zh_CNPath = "D:\\01MyFile\\赵政\\NBBOM\\bom\\动态域_zh_CN.voc";
        File dirs = new File("D:\\01MyFile\\赵政\\NBdomains") ;
        if(!dirs.exists()){
            System.out.println("创建目录："+dirs.getName());
            dirs.mkdirs();
        }
        System.out.println("======================开始解析:动态域_zh_CN.voc=============================");
        List<BOM> bomsList = ReadDomains.readzhCNFile(zh_CNPath);

        //3.合并两个文件中读取到的数据
        for (int i = 0; i < bomsList.size(); i++) {
            BOM bom = bomsList.get(i);
            if (bomMap.containsKey(bom.getClassName())) {
                System.out.println("找到" + bom.getClassName());
                BOM newBom = bomMap.get(bom.getClassName());
                for (Member member : bom.getMembers()) {
                    if (newBom.getMembersMap().containsKey(member.getCode())) {
                        Member newMember = newBom.getMembersMap().get(member.getCode());
                        member.setXOMValue(newMember.getXOMValue());
                    }
                }
            }
            //4.写入Excel
            ExcelWriter.writerFile(bom, dirs.getAbsolutePath());
        }
    }

    public static void testReadVoc(){
        String zh_CNPath = "D:\\workspaceIDEA\\MyCommonUtils\\src\\main\\resources\\动态域_zh_CN.voc";
        System.out.println("======================开始解析动态域_zh_CN.voc=============================");
        List<BOM> bomsList = ReadDomains.readzhCNFile(zh_CNPath);
        for (int i = 0; i < 10; i++) {
            System.out.println(bomsList.get(i));
        }
    }

    public static void testReadXML(){
        Map<String, BOM> bomMap = null;

        String xmlPath = "D:\\workspaceIDEA\\MyCommonUtils\\src\\main\\resources\\动态域.b2x";
        System.out.println("=================开始解析==========================================");
        bomMap = Dom4jExample.xmlToPojo(xmlPath);
        for(Map.Entry<String,BOM> entry:bomMap.entrySet()){
            System.out.println(entry.getKey());
            System.out.println(entry.getValue());
        }
    }




}
